package entity;

import app.MyFirstWebApplication;
import dao.impl.ClientDao;

public class Operations {

	public void block(int account, Client client) {
		for (CreditCard creditCard : client.getCreditCards()) {
			if (creditCard.getAccount() == account) {
				creditCard.setVariableLock(false);
			}
		}
	}
	
	public void unlock(int account, Client client) {
		for (CreditCard creditCard : client.getCreditCards()) {
			if (creditCard.getAccount() == account) {
				creditCard.setVariableLock(true);
			}
		}
	}
	
	public void pay(int amount, int account, int PIN, Client client)
	{
		for (CreditCard creditCard : client.getCreditCards()) {
			if (creditCard.getAccount() == account && creditCard.getPIN() == PIN) {
				if(creditCard.getBalance() - amount >= 0)
				creditCard.setBalance(creditCard.getBalance() - amount);
			}
		}
	}
	
	public void replenish(int amount, int account, int PIN, Client client)
	{
		for (CreditCard creditCard : client.getCreditCards()) {
			if (creditCard.getAccount() == account && creditCard.getPIN() == PIN) {
				creditCard.setBalance(creditCard.getBalance() + amount);
			}
		}
	}
	
	public Client addCreditCard(CreditCard creditCard, Client client) {
		ClientDao clientDao = new ClientDao(
				MyFirstWebApplication.DATA_XML_FOLDER);
		int count = 0;

		for (Client client1 : clientDao.getAll()) {
			for (CreditCard credit : client1.getCreditCards()) {
				if (credit.getAccount() == creditCard.getAccount()) {
					count++;
				}
			}
		}

		if (count == 0)
			client.getCreditCards().add(creditCard);

		return client;
	}
}
